package com.ztesoft.report.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztesoft.report.base.pojo.R;
import com.ztesoft.report.constant.enums.DictType;
import com.ztesoft.report.dao.SysDictDao;
import com.ztesoft.report.dao.SysSqlDao;
import com.ztesoft.report.entity.SysDict;
import com.ztesoft.report.entity.SysDictItem;
import com.ztesoft.report.entity.SysSql;
import com.ztesoft.report.service.DataService;
import com.ztesoft.report.service.SysDictService;
import com.ztesoft.report.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典管理
 *
 * @author 陈俊锋[296954345@qq.com]
 * @date 2018/6/12
 */
@Service
public class SysDictServiceImpl implements SysDictService {
    @Autowired
    SysDictDao sysDictDao;
    @Autowired
    SysSqlDao sysSqlDao;
    @Autowired
    DataService dataService;

    @Override
    public R queryPage(Map<String, Object> map) {
        int current = (int) map.get("current");
        int size = (int) map.get("size");
        PageHelper.startPage(current, size);
        List<SysDict> list = null;
        list = sysDictDao.queryPage(map);
        PageInfo<SysDict> page = new PageInfo<>(list);
        return R.ok(page);
    }

    @Override
    @Transactional
    @CacheEvict(value = "sys_dict_item", allEntries = true)
    public Map<String, Object> editData(SysDict sysDict) {
        sysDict.setUpdateDate(DateUtil.toDate(DateUtil.getDateTime(),"yyyyMMdd HH:mm:ss"));
        //判断是否重复主键
        if (!sysDict.getOldDictCode().equals(sysDict.getDictCode())) {
            int count = sysDictDao.countSysDictById(sysDict);
            if (count > 0) {
                throw new RuntimeException();
            }
        }
        sysDictDao.editData(sysDict);
        sysDictDao.editSysDictItem(sysDict);
        return R.ok();
    }

    @Override
    @Transactional
    @CacheEvict(value = "sys_dict_item", allEntries = true)
    public Map removeData(SysDict sysDict) {
        sysDict.setUpdateDate(new Date());
        sysDictDao.removeSysDictItem(sysDict);
        sysDictDao.removeData(sysDict);
        return R.ok();
    }

    @Override
    @CacheEvict(value = "sys_dict_item", allEntries = true)
    public Map addData(SysDict sysDict) {
        //SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //判断是否重复主键
        int count = sysDictDao.countSysDictById(sysDict);
        if (count > 0) {
            throw new RuntimeException();
        }
        sysDict.setCreateDate(new Date());
        sysDictDao.addData(sysDict);
        return R.ok();
    }

    @Override
    public Map sysDictList(Map<String, Object> map) {
        int current = (int) map.get("current");
        int size = (int) map.get("size");
        PageHelper.startPage(current, size);
        List<SysDictItem> list = null;
        list = sysDictDao.sysDictListEntity(map);
        PageInfo<SysDictItem> page = new PageInfo<>(list);
        return R.ok(page);
    }

    @Override
    @CacheEvict(value = "sys_dict_item", allEntries = true)
    public Map addSysDict(SysDictItem sysDictItem) {
        //判断是否重复主键
        int count = sysDictDao.countSysDictItemById(sysDictItem);
        if (count > 0) {
            throw new RuntimeException();
        }
        sysDictItem.setCreateDate(new Date());
        sysDictDao.addSysDict(sysDictItem);
        return R.ok();
    }

    @Override
    @CacheEvict(value = "sys_dict_item", allEntries = true)
    public Map editSysDictData(SysDictItem sysDictItem) {
        //判断是否重复主键
        if (!sysDictItem.getOldDictItemId().equals(sysDictItem.getDictItemId())) {
            int count = sysDictDao.countSysDictItemById(sysDictItem);
            if (count > 0) {
                throw new RuntimeException();
            }
        }
        sysDictItem.setUpdateDate(new Date());
        sysDictDao.editSysDictData(sysDictItem);
        return R.ok();
    }

    @Override
    @CacheEvict(value = "sys_dict_item", allEntries = true)
    public Map removeSysDict(SysDictItem sysDictItem) {
        sysDictItem.setUpdateDate(new Date());
        sysDictDao.removeSysDict(sysDictItem);
        return R.ok();
    }

    @Override
    @Cacheable(value = "sys_dict_item", key = "#sysDictItem.dictCode")
    public Map getSelectListByDictCode(SysDictItem sysDictItem) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dictCode", sysDictItem.getDictCode());
        List<SysDictItem> list = sysDictDao.sysDictListEntity(map);
        return R.ok(list);
    }

    @Override
    public Map getSelectListBySqlCode(SysSql sysSql) {
        SysSql temp = sysSqlDao.getSelectListBySqlCode(sysSql);
        List<Map> list = dataService.executeQuerySql(temp.getDatasourceId() + "", temp.getSqlText());
        return R.ok(list);
    }

    @Override
    @Cacheable(value = "sys_dict_item", key = "#code",condition="#type==1")
    public Map getDictByCodeAndValue(DictType dictType,String code,String value){
        if(dictType==DictType.DICT){
            Map<String, Object> mapParam = new HashMap<String, Object>();
            mapParam.put("dictCode",code);
            List<Map<String, Object>> list = sysDictDao.sysDictList(mapParam);
            for(Map map:list){
                if(value.equals(map.get("value").toString())){
                    return map;
                }
            }
        }else {
            SysSql sysSql =new SysSql();
            sysSql.setSqlCode(code);
            sysSql = sysSqlDao.getSelectListBySqlCode(sysSql);
            List<Map> list = dataService.executeQuerySql(sysSql.getDatasourceId() + "", sysSql.getSqlText());
            for(Map map:list){
                if(value.equals(map.get("value").toString())){
                    return map;
                }
            }
        }
        return null;
    }




}
